home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / PARENS.ICN < prev    next >
Text File  |  1992-11-26  |  3KB  |  111 lines

  1. ############################################################################
  2. #
  3. #    File:     parens.icn
  4. #
  5. #    Subject:  Program to produce random balanced strings
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     June 10, 1988
  10. #
  11. ###########################################################################
  12. #  
  13. #     This program produces parenthesis-balanced strings in which
  14. #  the parentheses are randomly distributed.
  15. #  
  16. #  Options: The following options are available:
  17. #  
  18. #       -b n Bound the length of the strings to n left and right
  19. #            parentheses each. The default is 10.
  20. #  
  21. #       -n n Produce n strings. The default is 10.
  22. #  
  23. #       -l s Use the string s for the left parenthesis. The default
  24. #            is ( .
  25. #  
  26. #       -r s Use the string s for the right parenthesis. The default
  27. #            is ) .
  28. #  
  29. #       -v   Randomly vary the length of the strings between 0 and
  30. #            the bound.  In the absence of this option, all strings
  31. #            are the exactly as long as the specified bound.
  32. #  
  33. #     For example, the output for
  34. #  
  35. #          parens -v -b 4 -l "begin " -r "end "
  36. #  
  37. #  is
  38. #  
  39. #          begin end
  40. #          begin end begin end
  41. #          begin begin end end begin end
  42. #          begin end begin begin end end
  43. #          begin end
  44. #          begin begin end end
  45. #          begin begin begin end end end
  46. #          begin end begin begin end end
  47. #          begin end begin end
  48. #          begin begin end begin end begin end end
  49. #  
  50. #  
  51. #  Comments: This program was motivated by the need for test data
  52. #  for error repair schemes for block-structured programming lan-
  53. #  gauges. A useful extension to this program would be some
  54. #  way of generating other text among the parentheses.  In addition
  55. #  to the intended use of the program, it can produce a variety of
  56. #  interesting patterns, depending on the strings specified by -l
  57. #  and -r.
  58. #  
  59. ############################################################################
  60. #
  61. #  Links: options
  62. #
  63. ############################################################################
  64.  
  65. link options
  66.  
  67. global r, k, lp, rp
  68.  
  69. procedure main(args)
  70.    local string, i, s, bound, limit, varying, opts
  71.    
  72.    bound := limit := 10            # default bound and limit
  73.    lp := "("                # default left paren
  74.    rp := ")"                # default right paren
  75.  
  76.    opts := options(args,"l:r:vb+n+")
  77.    bound := \opts["b"] | 10
  78.    limit := \opts["n"] | 10
  79.    lp := \opts["l"] | "("
  80.    rp := \opts["r"] | ")"
  81.    varying := opts["v"]
  82.    every 1 to limit do {
  83.       if \varying then k := 2 * ?bound else k := 2 * bound
  84.       string := ""
  85.       r := 0
  86.       while k ~= r do {
  87.          if r = 0 then string ||:= Open()
  88.          else if ?0 < probClose()
  89.             then string ||:= Close() else string ||:= Open()
  90.          }
  91.       while k > 0 do string ||:= Close()
  92.       write(string)
  93.       }
  94. end
  95.  
  96. procedure Open()
  97.    r +:= 1
  98.    k -:= 1
  99.    return lp
  100. end
  101.  
  102. procedure Close()
  103.    r -:= 1
  104.    k -:= 1
  105.    return rp
  106. end
  107.  
  108. procedure probClose()
  109.    return ((r * (r + k + 2)) / (2.0 * k * (r + 1)))
  110. end
  111.